public class Find {
    public boolean Find (int target, int[][] array) {
        // write code here
        int x = 0;
        int y = array[0].length - 1;
        if (array.length == 0 || y == 0) {
            return false;
        }
        while (x < array.length && y >= 0) {
            if (array[x][y] == target) {
                return true;
            } else if (array[x][y] < target) {
                x++;
            } else {
                int left = 0;
                int right = y;
                while (left < right) {
                    int mid = left + (right - left) / 2;
                    if (array[x][mid] < target) {
                        right = mid-1;
                    } else {
                        left = mid;
                    }
                }
                if (array[x][left] == target) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Find find = new Find();
        System.out.println(find.Find(7, new int[][]{{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}}));
    }
}
